#! /bin/bash

# Compare OS version and the specified one.
# $1 ... The specified OS major version.
# $2 ... The specified OS minor version.
# return ... 1 if the same or later, 0 if older.
isOlderOSVersion ()
{
	# Check the number of arguments.
	if [ $# -ne 2 ]; then
		echo "Invalid arguments."
		return 0
	fi
	
	# Get OS version.
	version=$( defaults read /System/Library/CoreServices/SystemVersion ProductVersion )
	if [ ! $version ]; then
		echo "Failed to get OS Version."
		return 0
	fi

	# Get OS major version.
	majorVersion=$( echo $version | cut -f1 -d"." )
	# Get OS minor version.
	minorVersion=$( echo $version | cut -f2 -d"." )

	# Compare the major version and the specified one.
	if [ $majorVersion -lt $1 ]; then
		echo "Mac OSX must be $1.$2 or later."
		return 0
	# Compare the minor version and the specified one, if the major versions are the same.
	elif [ $majorVersion -eq $1 -a $minorVersion -lt $2 ]; then
		echo "Mac OSX must be $1.$2 or later."
		return 0
	fi

	return 1
}

# Check if Mac is PPC-based.
# return ... 1 if Intel-based, 0 if PPC-based.
isPPCBased ()
{
	architecture=`arch`
	if [ $architecture = "ppc" -o $architecture = "ppc64" ]; then
		echo "PPC is not supported."
		return 0
	fi

	return 1
}


# Check OS version.
if isOlderOSVersion 10 5; then
	exit 97
fi

# Check if Mac is Intel-based.
if isPPCBased; then
	exit 97
fi


# Remove the Receipt to allow down-grade installation.
rm -rf '/Library/Receipts/Yamaha DTX900 Extension.pkg'

exit 0
